package com.rub.android.ui.activity


import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.rub.android.R
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer


/**
 * 简单详情实现模式2
 * 横屏不旋转的 Demo
 */
class VideoPlayerDetailActivity : AppCompatActivity() {
    var detailPlayer: StandardGSYVideoPlayer? = null
    private var isPlay = false
    private var isPause = false

    //private var orientationUtils: OrientationUtils

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_simple_detail_player)
        detailPlayer = findViewById<View>(R.id.detail_player) as StandardGSYVideoPlayer
        val url = "http://7xjmzj.com1.z0.glb.clouddn.com/20171026175005_JObCxCE2.mp4"

        //增加封面
        val imageView = ImageView(this)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        imageView.setImageResource(R.mipmap.ic_launcher)

        //增加title
        detailPlayer!!.titleTextView.visibility = View.GONE
        detailPlayer!!.backButton.visibility = View.GONE

        //detailPlayer!!.setSpeed(mSpeed,mSounchTouch)

        //外部辅助的旋转，帮助全屏
        //orientationUtils = new OrientationUtils(this, detailPlayer);
        //初始化不打开外部的旋转
        // orientationUtils.setEnable(false);
        val gsyVideoOption = GSYVideoOptionBuilder()

        with(gsyVideoOption) {
            setThumbImageView(imageView)
                .setIsTouchWiget(true)
                .setRotateViewAuto(false)
                .setLockLand(false)
                .setAutoFullWithSize(false)
                .setShowFullAnimation(false)
                .setNeedLockFull(true)
                .setUrl(url)
                .setCacheWithPlay(false)
                .setVideoTitle("测试视频")
                .setVideoAllCallBack(object : GSYSampleCallBack() {
                    override fun onPrepared(url: String, vararg objects: Any) {
                        super.onPrepared(url, *objects)
                        //开始播放了才能旋转和全屏
                        //orientationUtils.setEnable(detailPlayer.isRotateWithSystem());
                        isPlay = true
                    }

                    override fun onQuitFullscreen(url: String, vararg objects: Any) {
                        super.onQuitFullscreen(url, *objects)
                        // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
                        // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
                        // if (orientationUtils != null) {
                        //    orientationUtils.backToProtVideo()
                        // }
                    }
                }).setLockClickListener { _, _ ->
                    // if (orientationUtils != null) {
                    //   //配合下方的onConfigurationChanged
                    //    orientationUtils.setEnable(!lock)
                    // }
                }.build(detailPlayer)
        }
        detailPlayer!!.fullscreenButton.setOnClickListener {
            //直接横屏
            // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
            // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
            // orientationUtils.resolveByClick();
            //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
            detailPlayer!!.startWindowFullscreen(this@VideoPlayerDetailActivity, true, true)
        }
    }

    override fun onBackPressed() {
        // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
        // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
        //        if (orientationUtils != null) {
        //            orientationUtils.backToProtVideo();
        //        }
        if (GSYVideoManager.backFromWindowFull(this)) {
            return
        }
        super.onBackPressed()
    }

    override fun onPause() {
        detailPlayer!!.currentPlayer.onVideoPause()
        super.onPause()
        isPause = true
    }

    override fun onResume() {
        detailPlayer!!.currentPlayer.onVideoResume(false)
        super.onResume()
        isPause = false
    }

    override fun onDestroy() {
        super.onDestroy()
        if (isPlay) {
            detailPlayer!!.currentPlayer.release()
        }
        // if (orientationUtils != null)
        //  orientationUtils.releaseListener()
    }

    /**
     * orientationUtils 和  detailPlayer.onConfigurationChanged 方法是用于触发屏幕旋转的
     */
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        //如果旋转了就全屏
        //        if (isPlay && !isPause) {
        //            detailPlayer.onConfigurationChanged(this, newConfig, orientationUtils, true, true)
        //        }
    }
}